﻿Imports System.Runtime.InteropServices
Imports System.IO
Imports Microsoft.Office.Tools.Ribbon
Imports Powerpoint = Microsoft.Office.Interop.PowerPoint
Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class Ribbon1

    Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load

    End Sub

    Public Sub SendSlides(ByVal saveFileName As String, ByVal useTemplate As Boolean, ByVal withAllSlides As Boolean)
        Dim outlook As Outlook.Application = Nothing

        Try
            Dim fileName As String = CreatePresentation(saveFileName, useTemplate, False, withAllSlides)

            Try
                '// Get hold of Outlook if it is running already
                Dim app As Object = Marshal.GetActiveObject("Outlook.Application")
                outlook = CType(app, Outlook.Application)
            Catch e1 As COMException

            End Try

            If outlook Is Nothing Then
                '// If Outlook is not already running, launch it
                outlook = New Outlook.Application()
            End If

            If outlook IsNot Nothing Then
                Dim mail As Outlook.MailItem = CType(outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem), Outlook.MailItem)
                mail.Subject = "Slides " & fileName.Substring(fileName.LastIndexOf("\"c) + 1, fileName.LastIndexOf("."c) - fileName.LastIndexOf("\"c) - 1)
                mail.Body = "Your message is ready to be sent with the following attachment: " & fileName.Substring(fileName.LastIndexOf("\"c) + 1)
                mail.Display(False)

                mail.Attachments.Add(CObj(fileName), CObj(Microsoft.Office.Interop.Outlook.OlAttachmentType.olOLE), CObj(1), CObj("Nothing"))
            Else
                System.Windows.Forms.MessageBox.Show("Unable to process the request")
            End If

        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Function CreatePresentation(ByVal saveFileName As String, ByVal useTemplate As Boolean,
                                        ByVal displaySlides As Boolean, ByVal withAllSlides As Boolean) As String
        Dim fileName As String = ""

        '// Create a presentation
        Dim pres As Powerpoint.Presentation = Globals.ThisAddIn.Application.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse)

        '// Only save and apply the current template if the user chose to
        If useTemplate Then
            '// Apply the selected template to the new deck
            fileName = Path.GetTempFileName()
            Globals.ThisAddIn.Application.ActivePresentation.SaveAs(fileName, Powerpoint.PpSaveAsFileType.ppSaveAsOpenXMLTheme,
                                                                    Microsoft.Office.Core.MsoTriState.msoTrue)
            pres.ApplyTemplate(fileName)
        End If

        If Not withAllSlides Then
            Globals.ThisAddIn.Application.ActiveWindow.Selection.SlideRange.Copy()
        Else
            Globals.ThisAddIn.Application.ActiveWindow.Presentation.Slides.Range().Copy()
        End If

        pres.Slides.Paste()

        '// Check if new deck should be saved
        If Not displaySlides Then
            '// Get a temp filename
            fileName = Path.GetTempFileName()
            If saveFileName = "" Then
                fileName = fileName.Substring(0, fileName.LastIndexOf("."c))
                fileName &= ".pptx"
            Else
                fileName = fileName.Substring(0, fileName.LastIndexOf("\"c))
                fileName &= "\" & saveFileName & ".pptx"
            End If

            '// Write the output to disk
            pres.SaveAs(fileName, Powerpoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentation, Microsoft.Office.Core.MsoTriState.msoFalse)

            pres.Close()
        Else
            '// Force application to be visible
            pres.NewWindow()
        End If

        Return fileName

    End Function

    Private Sub rxbExtract_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles rxbExtract.Click

        CreatePresentation("", True, True, False)

    End Sub

    Private Sub rxbtnSendSelected_Click(sender As Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles rxbtnSendSelected.Click

        SendSlides(rxtxtFilename.Text.Trim(), rxchkTemplate.Checked, False)

    End Sub


    Private Sub rxbtnSendAll_Click(sender As Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles rxbtnSendAll.Click

        SendSlides(rxtxtFilename.Text.Trim(), rxchkTemplate.Checked, True)

    End Sub

End Class
